package com.ironman.imes.device.framework.base;

import com.ironman.imes.device.framework.base.enums.IErrorCode;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.List;

/**
 * description
 * company 铁人科技
 *
 * @author 陈浩杰
 * @date 2022/9/8 22:47
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Res implements Serializable {

    private static final long serialVersionUID = -3243475556181265119L;

    private boolean success;

    private String errCode;

    private String errMessage;

    private Object data;

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class Page {

        private Long currentPage;

        private Long totalCount;

        private Long totalPage;

        private boolean previous;

        private boolean next;

        private List<?> records;

        public Page(Long currentPage, Long totalCount, List<?> records) {
            this.currentPage = currentPage;
            this.totalCount = totalCount;
            this.records = records;
        }
    }

    public static Res ok() {
        return new Res(true, "", "", null);
    }

    public static Res ok(Object data) {
        return new Res(true, "", "", data);
    }

    public static Res error(String errMessage) {
        return new Res(false, "", errMessage, null);
    }

    public static Res error(String errMessage, Object data) {
        return new Res(false, "", errMessage, data);
    }

    public static Res error(String errCode, String errMessage) {
        return new Res(false, errCode, errMessage, null);
    }

    public static Res error(String errCode, String errMessage, Object data) {
        return new Res(false, errCode, errMessage, data);
    }

    public static Res error(IErrorCode iErrorCode) {
        return new Res(false, iErrorCode.getCode(), iErrorCode.getMessage(), null);
    }

    public static Res error(IErrorCode iErrorCode, String errMessage) {
        return new Res(false, iErrorCode.getCode(), errMessage, null);
    }

    /**
     * @param currentPage 当前页
     * @param totalCount  总记录数
     * @param records     获取每页数据的list集合
     * @return
     */
    public static Res pageOf(Long currentPage, Long totalCount, List<?> records) {
        return Res.ok(new Page(currentPage, totalCount, records));
    }

    /**
     * @param currentPage 当前页
     * @param totalCount  总记录数
     * @param totalPage   总页数
     * @param previous    是否有上一页
     * @param next        是否有下一页
     * @param records     获取每页数据的list集合
     * @return
     */
    public static Res pageOf(Long currentPage, Long totalCount, Long totalPage, boolean previous, boolean next, List<?> records) {
        return Res.ok(new Page(currentPage, totalCount, totalPage, previous, next, records));
    }
}